package routes

import (
	"github.com/gin-gonic/gin"
	"github.com/inntu/backend/api/controllers"
	"github.com/inntu/backend/api/middleware"
)

// SetupAPIV1Routes 设置API v1路由
func SetupAPIV1Routes(router *gin.Engine) {
	// 创建控制器实例
	pageController := controllers.NewPageController()
	categoryController := controllers.NewCategoryController()
	itemController := controllers.NewItemController()
	userController := controllers.NewUserController()
	uploadController := controllers.NewUploadController()

	// 健康检查端点（无需认证）
	router.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"status":  "ok",
			"message": "Backend service is running",
			"timestamp": gin.H{
				"unix": gin.H{},
			},
		})
	})

	// API v1 路由组
	v1 := router.Group("/api/v1")
	{
		// 健康检查API
		v1.GET("/health", func(c *gin.Context) {
			c.JSON(200, gin.H{
				"status":  "ok",
				"message": "API v1 is running",
				"version": "1.0",
			})
		})

		// 用户认证API（无需认证）
		v1.POST("/auth/login", userController.Login)

		// 小程序端和管理端共用的API（可选认证）
		public := v1.Group("/")
		public.Use(middleware.OptionalJWT())
		{
			// 分类相关API（小程序端需要）
			public.GET("/categories", categoryController.GetCategories)
			public.GET("/categories/:id", categoryController.GetCategory)
			public.GET("/categories/:id/items", categoryController.GetCategoryWithItems)

			// 项目相关API（小程序端需要）
			public.GET("/items", itemController.GetItems)
			public.GET("/items/:id", itemController.GetItem)
			public.GET("/items/:id/category", itemController.GetItemWithCategory)
			public.GET("/items/:id/children", itemController.GetChildItems)
			public.GET("/items/:id/related", itemController.GetItemWithRelatedItems)
			public.GET("/items/:id/relations", itemController.GetPageItemRelations)

			// 页面相关API（小程序端需要）
			public.GET("/pages", pageController.GetPages)
			public.GET("/pages/code/:code", pageController.GetPageByCode)
			public.GET("/pages/:code/categories", pageController.GetPageWithCategories)
			public.GET("/hierarchy", pageController.GetHierarchyData)
		}

		// 页面数据API（兼容旧版本，不需要认证）
		v1.GET("/page/:page_name", pageController.GetPageData)

		// 管理端专用API（严格认证）
		admin := v1.Group("/admin")
		admin.Use(middleware.JWT())
		{
			// 获取当前用户信息
			admin.GET("/auth/user-info", userController.GetUserInfo)

			// 分类管理端 CRUD
			admin.GET("/categories", categoryController.GetCategories)
			admin.GET("/categories/:id", categoryController.GetCategory)
			admin.POST("/categories", categoryController.CreateCategory)
			admin.PUT("/categories/:id", categoryController.UpdateCategory)
			admin.DELETE("/categories/:id", categoryController.DeleteCategory)

			// 项目管理端 CRUD
			admin.GET("/items", itemController.GetItems)
			admin.GET("/items/:id", itemController.GetItem)
			admin.POST("/items", itemController.CreateItem)
			admin.PUT("/items/:id", itemController.UpdateItem)
			admin.DELETE("/items/:id", itemController.DeleteItem)

			// 页面管理端 CRUD
			admin.GET("/pages", pageController.GetPages)
			admin.GET("/pages/:id", pageController.GetPageByID)
			admin.POST("/pages", pageController.CreatePage)
			admin.PUT("/pages/:id", pageController.UpdatePage)
			admin.DELETE("/pages/:id", pageController.DeletePage)

			// 层级数据管理API
			admin.GET("/hierarchy/categories", pageController.GetAvailableCategories)
			admin.GET("/hierarchy/items", pageController.GetAvailableItems)
			// 页面-分类关系管理
			admin.POST("/hierarchy/pages/:pageId/categories/:categoryId", pageController.AddCategoryToPage)
			admin.DELETE("/hierarchy/pages/:pageId/categories/:categoryId", pageController.RemoveCategoryFromPage)
			// 分类-项目关系管理
			admin.POST("/hierarchy/categories/:categoryId/items/:itemId", categoryController.AddItemToCategory)
			admin.DELETE("/hierarchy/categories/:categoryId/items/:itemId", categoryController.RemoveItemFromCategory)

			// 页面项目关联关系管理
		admin.POST("/page-items/:page_item_id/related/:item_id", itemController.AddItemToPageItem)
		admin.DELETE("/page-items/:page_item_id/related/:item_id", itemController.RemoveItemFromPageItem)
		admin.PUT("/page-items/:page_item_id/related/:item_id/order", itemController.UpdatePageItemRelationOrder)

		// 文件上传API
		admin.POST("/upload", uploadController.UploadFile)
		admin.POST("/upload/image", uploadController.UploadImage)
	}

		// 已移除内容相关路由（兼容旧版API）
	}
}
